package thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;


/**
 *
 */
public class ThreadPoolTest {


    public static void demo() {
        ThreadPoolExecutor SynchronousQueueexecutorService =
                (ThreadPoolExecutor) Executors.newCachedThreadPool(new MyThreadFactory("newCachedThreadPool-"));

        ThreadPoolExecutor LinkedBlockingQueueexecutorService =
                (ThreadPoolExecutor) Executors.newFixedThreadPool(10, new MyThreadFactory("newCachedThreadPool-"));


        SynchronousQueueexecutorService.submit(() -> System.out.println(Thread.currentThread().getName()));
        LinkedBlockingQueueexecutorService.submit(() -> System.out.println(Thread.currentThread().getName()));
    }

    public static void main(String[] args) {

    }
}

class MyThreadFactory implements ThreadFactory {

    AtomicInteger atomicInteger = new AtomicInteger(0);
    String threadNamePrefix;

    public MyThreadFactory(String threadNamePrefix) {
        this.threadNamePrefix = threadNamePrefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, threadNamePrefix + atomicInteger.getAndIncrement());
    }
}